const del = require('del')
const gulp = require('gulp')
const babelify = require('babelify')
const browserify = require('browserify')
const watchify = require('watchify')
const buffer = require('vinyl-buffer')
const source = require('vinyl-source-stream')
const sourcemaps = require('gulp-sourcemaps')

const browserSync = require('browser-sync').create()

function doBundle(b) {
    return b.bundle()
        .on('error', console.error.bind(console))
        .pipe(source('flv.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./dist/'))
}

gulp.task('clean', function () {
    return del([
        'dist/*'
    ])
})

gulp.task('build', function () {
    let b = browserify({
        entries: 'src/index.js',
        standalone: 'flvjs',
        debug: true,
        transform: ['babelify', 'browserify-versionify'],
        plugin: ['browserify-derequire']
    })
    return doBundle(b)
})
